package com.hyy.libray.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.hyy.libray.R;
import com.hyy.libray.contract.BaseContract;
import com.hyy.libray.presenter.IBasePresenter;
import com.hyy.libray.ui.view.LottieEmpyCallback;
import com.hyy.libray.ui.view.LottieLoadingCallback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.ButterKnife;


public abstract class BaseActivity<T extends IBasePresenter> extends AppCompatActivity implements BaseContract.IView {


    public static final String TAG = BaseActivity.class.getSimpleName();

    public T mPresenter;

    private LoadService loadService;
    public TextView tvTitle;
    private TextView tvRight;
    private RelativeLayout llTitlebar;
    public ImageView imgBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFullScreen();
        setContentView(getLayoutResId());
        ButterKnife.bind(this);
        mPresenter = onBasePresenter();
        loadService = LoadSir.getDefault().register(getLoadSirTarget());
        loadService.showSuccess();
        initImmersionBar();
        attachView();
        initView();
        initData();
    }


    public void initTitle(String titleStr) {
        initTitle(titleStr, R.color.title_top_color);
    }

    public void initTitle(String titleStr, BarHide barHide) {
        initTitle(titleStr, R.color.title_top_color, barHide);
    }

    public void initTitle(String titleStr, int statusBarColor, BarHide barHide) {
        initTitle(titleStr, statusBarColor, 0, barHide);
    }

    public void initTitle(String titleStr, int statusBarColor) {
        initTitle(titleStr, statusBarColor, 0, BarHide.FLAG_SHOW_BAR);
    }

    public void initTitle(String titleStr, int statusBarColor, @DrawableRes int titleBarBgColor, BarHide barHide) {
        View rlTopView = findViewById(R.id.view_statusbar);
        if (rlTopView != null) {
            ImmersionBar.with(this)
                    .statusBarView(rlTopView)
                    .hideBar(barHide)
                    .statusBarColor(statusBarColor)
                    .init();
        }

        llTitlebar = findViewById(R.id.ll_titlebar);

        if (llTitlebar != null && titleBarBgColor != 0) {
            llTitlebar.setBackgroundResource(titleBarBgColor);
        }
        tvTitle = findViewById(R.id.tv_title);
        if (tvTitle != null) {
            tvTitle.setText(titleStr);
        }
        imgBack = findViewById(R.id.img_back);
        if (imgBack != null) {
            imgBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    public void initTitle(String titleStr, String rightStr, int rightColor, View.OnClickListener rightOnClickListener) {
        View rlTopView = findViewById(R.id.view_statusbar);
        if (rlTopView != null) {
            ImmersionBar.with(this)
                    .statusBarView(rlTopView)
                    .statusBarColor(R.color.title_top_color)
                    .init();
        }

        tvRight = findViewById(R.id.tv_right);
        if (tvRight != null) {
            tvRight.setVisibility(View.VISIBLE);
            tvRight.setText(rightStr);
            tvRight.setTextColor(rightColor);
            tvRight.setOnClickListener(rightOnClickListener);
        }

        tvTitle = findViewById(R.id.tv_title);
        if (tvTitle != null) {
            tvTitle.setText(titleStr);
        }
        imgBack = findViewById(R.id.img_back);
        if (imgBack != null) {
            imgBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }


    /**
     * 初始化沉浸式
     * Init immersion bar.
     */
    public void initImmersionBar() {

    }

    public Object getLoadSirTarget() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dettachView();
    }


    public void attachView() {
        if (mPresenter != null) {
            mPresenter.onttach(this);
        }
    }


    public void dettachView() {
        if (mPresenter != null) {
            mPresenter.dettach();
        }
    }


    @Override
    public void showLoading() {
        if (loadService != null) {
            loadService.showCallback(LottieLoadingCallback.class);
        }
    }

    @Override
    public void showErrorView(int errorResult) {
        if (loadService != null) {
            loadService.showCallback(LottieEmpyCallback.class);
        }
    }


    @Override
    public void disLoading() {
        if (loadService != null) {
            loadService.showSuccess();
        }
    }


    /**
     * 获取布局文件地址
     */
    public abstract int getLayoutResId();

    /**
     * 初始化布局
     */
    public abstract void initView();


    /**
     * 初始化数据
     */
    public void initData() {

    }


    /**
     * 获取Presenter，不用MVP，可以为空
     *
     * @return
     */
    protected abstract T onBasePresenter();

    public void initFullScreen() {
    }

}
